package com.briup.jz.web.controller;

import com.briup.jz.bean.User;
import com.briup.jz.bean.extend.UserExtend;
import com.briup.jz.service.IUserService;
import com.briup.jz.utils.Message;
import com.briup.jz.utils.MessageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.List;

/**
 * @program: jz
 * @description: 用户控制器
 * @author: 丁梦迪（git名字冲突，提交更新一下）
 * @create: 2020-06-11 15:59
 **/
@Api(description = "用户管理接口")
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private IUserService userService;

    @ApiOperation(value="查找所有")
    @GetMapping("findAll")
    public Message findAll(){
        List<User> list = userService.findAll();
        return MessageUtil.success(list);

    }
    @ApiOperation(value = "查找所有员工")
    @GetMapping("findAllEmployee")
    public Message findAllEmployee(){
        List<UserExtend> list = userService.findAllEmployee();
        return MessageUtil.success(list);

    }

    @ApiOperation(value = "通过ID进行审核通过")
    @ApiImplicitParam(name = "id", value = "审核唯一编号", required = true, paramType = "query",dataType = "long")
    @GetMapping("auditing")
    public Message auditing(long id){
        User user = userService.findById(id);
        user.setStatus("启用");
        userService.saveOrUpdate(user);
        return MessageUtil.success("审核成功");
    }

    @ApiOperation(value = "通过ID进行审核拒绝")
    @ApiImplicitParam(name = "id", value = "审核唯一编号", required = true, paramType = "query",dataType = "long")
    @GetMapping("refuseauditing")
    public Message refuseauditing(long id){
        User user = userService.findById(id);
        user.setStatus("禁用");
        userService.saveOrUpdate(user);
        return MessageUtil.success("已拒绝审核");
    }

    @ApiOperation(value="保存或更新")
    @PostMapping("saveOrUpdate")
    public Message saveOrUpdate(User user){
        userService.saveOrUpdate(user);
        return MessageUtil.success("保存成功");
    }


    @ApiOperation(value="通过Id删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value="唯一标号", required = true, paramType="query")
    })
    @GetMapping("deleteById")
    public Message deleteById(long id){
        userService.deleteById(id);
        return MessageUtil.success("删除成功");
    }


    /*
     * 查询所有用户 并且获得用户角色
     * */
    @ApiOperation(value="查找所有及属性")
    @GetMapping("findAllWithRole")
    public Message findAllWithRole(){
        List<UserExtend> list = userService.findAllWithRole();
        return MessageUtil.success(list);
    }
}